package com.cpe.web.controller.physique;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpe.data.domain.physique.score.PhysiqueScore;
import com.cpe.data.domain.physique.score.PhysiqueScoreInfo;
import com.cpe.data.domain.physique.score.PhysiqueScoreQuery;
import com.cpe.data.domain.physique.score.TempCheckQuery;
import com.cpe.repository.mapper.physique.PhysiqueScoreMapper;
import com.cpe.transfer.physique.PhysiqueScoreTransfer;
import com.google.common.collect.Lists;
import com.sql.data.response.JsonResponse;
import com.util.math.CalcUtil;

@RestController
@RequestMapping("/physique_scores")
public class PhysiqueScoreController {

	@Resource
	private PhysiqueScoreMapper physiqueScoreMapper;

	@Resource
	private PhysiqueScoreTransfer physiqueScoreTransfer;

	/**
	 * 自检查询
	 */
	@RequestMapping(value = "/temp_check", method = GET)
	public JsonResponse tempCheck(TempCheckQuery query) throws ParseException {
		List<PhysiqueScoreInfo> infos = Lists.newArrayList();
		PhysiqueScoreQuery scoreQuery = new PhysiqueScoreQuery();

		// 参数校验
		if (query.getBirthday() == null) {
			return JsonResponse.invalid("出生日期不能为空！");
		} else {
			scoreQuery.setAge(CalcUtil.getRealAge(new SimpleDateFormat("yyyy-MM-dd").parse(query.getBirthday())));
		}

		if (query.getHeight() == null) {
			return JsonResponse.invalid("身高不能为空！");
		} else {
			scoreQuery.setScore(query.getHeight());
		}

		if (query.getWeight() == null) {
			return JsonResponse.invalid("体重不能为空！");
		} else {
			scoreQuery.setScore2(query.getWeight());
		}

		if (query.getSex() == null) {
			return JsonResponse.invalid("性别不能为空！");
		} else {
			scoreQuery.setSex(query.getSex());
		}

		// 查询体型项
		scoreQuery.setPhysiqueName("体型");
		PhysiqueScore physiqueScore1 = physiqueScoreMapper.findOneByCustomQuery(scoreQuery);
		if (physiqueScore1 != null) {
			PhysiqueScoreInfo info1 = physiqueScoreTransfer.toInfo(physiqueScore1);
			info1.setLowOrHigh(physiqueScoreTransfer
					.calcShapeValue(query.getHeight(), query.getWeight(), query.getSex(), scoreQuery.getAge())
					.getValue1());
			infos.add(info1);
		}

		// 查询身高项
		scoreQuery.setPhysiqueName("身高");
		scoreQuery.setScore2(null);
		PhysiqueScore physiqueScore2 = physiqueScoreMapper.findOneByCustomQuery(scoreQuery);
		if (physiqueScore2 != null) {
			PhysiqueScoreInfo info2 = physiqueScoreTransfer.toInfo(physiqueScore2);
			infos.add(info2);
		}
		return JsonResponse.ok(infos);
	}
}
